package cn.iocoder.yudao.module.crm.controller.admin.receipt;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.crm.controller.admin.receipt.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.receipt.ReceiptDO;
import cn.iocoder.yudao.module.crm.service.receipt.ReceiptService;


@Tag(name = "管理后台 - crm发票管理")
@RestController
@RequestMapping("/crm/receipt")
@Validated
public class ReceiptController {

    @Resource
    private ReceiptService receiptService;

    @PostMapping("/create")
    @Operation(summary = "创建crm发票管理")
    @PreAuthorize("@ss.hasPermission('crm:receipt:create')")
    public CommonResult<Long> createReceipt(@Valid @RequestBody ReceiptSaveReqVO createReqVO) {
        return success(receiptService.createReceipt(createReqVO, getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新crm发票管理")
    @PreAuthorize("@ss.hasPermission('crm:receipt:update')")
    public CommonResult<Boolean> updateReceipt(@Valid @RequestBody ReceiptSaveReqVO updateReqVO) {
        receiptService.updateReceipt(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除crm发票管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:receipt:delete')")
    public CommonResult<Boolean> deleteReceipt(@RequestParam("id") Long id) {
        receiptService.deleteReceipt(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除crm发票管理")
                @PreAuthorize("@ss.hasPermission('crm:receipt:delete')")
    public CommonResult<Boolean> deleteReceiptList(@RequestParam("ids") List<Long> ids) {
        receiptService.deleteReceiptListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得crm发票管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:receipt:query')")
    public CommonResult<ReceiptRespVO> getReceipt(@RequestParam("id") Long id) {
        ReceiptDO receipt = receiptService.getReceipt(id);
        return success(BeanUtils.toBean(receipt, ReceiptRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得crm发票管理分页")
    @PreAuthorize("@ss.hasPermission('crm:receipt:query')")
    public CommonResult<PageResult<ReceiptRespVO>> getReceiptPage(@Valid ReceiptPageReqVO pageReqVO) {
        PageResult<ReceiptDO> pageResult = receiptService.getReceiptPage(pageReqVO, getLoginUserId());
        return success(BeanUtils.toBean(pageResult, ReceiptRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出crm发票管理 Excel")
    @PreAuthorize("@ss.hasPermission('crm:receipt:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReceiptExcel(@Valid ReceiptPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReceiptDO> list = receiptService.getReceiptPage(pageReqVO,  getLoginUserId()).getList();
        // 导出 Excel
        ExcelUtils.write(response, "crm发票管理.xls", "数据", ReceiptRespVO.class,
                        BeanUtils.toBean(list, ReceiptRespVO.class));
    }

    @PutMapping("/submit")
    @Operation(summary = "提交发票审批")
    @PreAuthorize("@ss.hasPermission('crm:receipt:update')")
    public CommonResult<Boolean> submitReceipt(@RequestParam("id") Long id) {
        receiptService.submitReceipt(id, getLoginUserId());
        return success(true);
    }

    @GetMapping("/audit-count")
    @Operation(summary = "获得待审核发票数量")
    @PreAuthorize("@ss.hasPermission('crm:receipt:query')")
    public CommonResult<Long> getAuditReceiptCount() {
        return success(receiptService.getAuditReceiptCount(getLoginUserId()));
    }

}